﻿<%@ WebHandler Language="C#" Class="UpdateSchedules" %>

using System;
using System.Web;
using System.Linq;
using System.Data.Linq;

public class UpdateSchedules : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        int nPermissions = 0;
        if (context.Session["logined_id"] != null)
        {
            if (context.Session["Schedules"] != "") nPermissions = int.Parse(context.Session["Schedules"].ToString());
        }

        if (nPermissions < 3)
        {
            context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new
            {
                Message = "Access Denied",
                Date = DateTime.Now.ToString("dd-MM-yyyy")
            }));
            return;
        }


        string items = context.Request.Params["items"];
        if (!string.IsNullOrEmpty(items))
        {
            System.Collections.Generic.List<LdExpress.Schedules> udpitems = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<LdExpress.Schedules>>(items);
            string ids = "";
            string carrsname = "";
            string portsname = "";
            ids = string.Join(",", udpitems.Select(q => q.ID).Distinct());
            System.Data.DataSet ds = new System.Data.DataSet();
            string condition = "";
            if (!string.IsNullOrEmpty(ids))
            {
                condition = " and ID in (" + ids + ")";
            }
            else
            {
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new
                {
                    Message = "Not valid",
                    Date = DateTime.Now.ToString("dd-MM-yyyy")
                }));
                return;
            }
            int nNumOfPages;
            LdExpress.Schedules.GetList(condition, "", 999, 1, out nNumOfPages, out ds, LdExpress.GV.cn, LdExpress.GV.trans);

            carrsname = string.Join(",", udpitems.Where(q => q.CarrName != null && q.CarrName.ToString() != "").Select(q => "'" + LdExpress.DBModule.RefineString(HttpUtility.HtmlDecode(q.CarrName.ToString())) + "'").Distinct());
            portsname = string.Join(",", udpitems.Where(q => q.CarrName != null && q.CarrName.ToString() != "").Select(q => "'" + LdExpress.DBModule.RefineString(HttpUtility.HtmlDecode(q.PortFromName.ToString())) + "'").Union(udpitems.Select(q => "'" + LdExpress.DBModule.RefineString(HttpUtility.HtmlDecode(q.PortToName.ToString())) + "'")).Distinct());

            System.Data.DataSet dscarr = new System.Data.DataSet();
            System.Data.DataSet dsport = new System.Data.DataSet();

            LdExpress.Ports.GetList(" and tbl_ports.name in (" + portsname + ")", "", 999, 1, out nNumOfPages, out dsport, LdExpress.GV.cn, LdExpress.GV.trans);

            LdExpress.Carrs.GetList(" and tbl_carrs.name in (" + carrsname + ")", "", 999, 1, out nNumOfPages, out dscarr, LdExpress.GV.cn, LdExpress.GV.trans);

            var carrArr = (from System.Data.DataRow item in dscarr.Tables[0].Rows
                           select new { ID = long.Parse(item["ID"].ToString()), Name = item["Name"] }).ToList();



            var portArr = (from System.Data.DataRow item in dsport.Tables[0].Rows
                           select new { ID = long.Parse(item["ID"].ToString()), Name = item["Name"] }).ToList();

            context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(portArr));
            context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(carrArr));

            Array arr = udpitems.ToArray();
            if (ds.Tables.Count > 0)
            {
                LdExpress.Schedules sch;
                LdExpress.Schedules udpitem;
                DateTime _now = DateTime.Now;
                foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
                {
                    long ID = long.Parse(dr["ID"].ToString());
                    sch = new LdExpress.Schedules();
                    udpitem = udpitems.Where(p => p.ID == ID).FirstOrDefault();
                    if (udpitem == null)
                        continue;

                    context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(udpitem.PortToName));
                    context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(udpitem.PortFromName));

                    var carritem = carrArr.Where(p => p.Name.ToString().ToLower() == HttpUtility.HtmlDecode(udpitem.CarrName.ToString()).ToLower()).FirstOrDefault();
                    var porttoitem = portArr.Where(p => p.Name.ToString().ToLower() == udpitem.PortToName.ToString().ToLower()).FirstOrDefault();
                    //var portfromitem = portArr.Where(p => p.Name.ToString().ToLower() ==HttpUtility.HtmlDecode( udpitem.PortFromName.ToString()).ToLower()).FirstOrDefault();
                    var portfromitem = portArr.Where(p => p.Name.ToString().ToLower() == HttpUtility.HtmlDecode(udpitem.PortFromName.ToString()).ToLower()).FirstOrDefault();
                    sch.ID = ID;
                    sch.Load(LdExpress.GV.cn, LdExpress.GV.trans);
                    //sch.CarrID = (carritem != null ? carritem.ID : -1);
                    if (carritem != null)
                        sch.CarrID = carritem.ID;
                    sch.PortToID = (porttoitem != null ? porttoitem.ID : -1);
                    sch.PortFromID = (portfromitem != null ? portfromitem.ID : -1);
                    sch.Service = HttpUtility.HtmlDecode(udpitem.Service);
                    sch.Mon = HttpUtility.HtmlDecode(udpitem.Mon);
                    sch.Tue = HttpUtility.HtmlDecode(udpitem.Tue);
                    sch.Web = HttpUtility.HtmlDecode(udpitem.Web);
                    sch.Thu = HttpUtility.HtmlDecode(udpitem.Thu);
                    sch.Fri = HttpUtility.HtmlDecode(udpitem.Fri);
                    sch.Sat = HttpUtility.HtmlDecode(udpitem.Sat);
                    sch.Sun = HttpUtility.HtmlDecode(udpitem.Sun);
                    sch.TS1 = HttpUtility.HtmlDecode(udpitem.TS1);
                    sch.TS2 = HttpUtility.HtmlDecode(udpitem.TS2);
                    sch.CY = HttpUtility.HtmlDecode(udpitem.CY);
                    sch.CLS = HttpUtility.HtmlDecode(udpitem.CLS);
                    sch.Remark = HttpUtility.HtmlDecode(udpitem.Remark);
                    sch.Region = HttpUtility.HtmlDecode(udpitem.Region);
                    sch.LastUpdate = _now;

                    sch.Description = dr["Description"].ToString();
                    sch.Import = dr["Import"].ToString();
                    sch.Sort = (int)dr["Sort"];

                    sch.Save(LdExpress.GV.cn, LdExpress.GV.trans);
                }
                LdExpress.UserObjectsLog.SaveObjectActionLog(long.Parse(context.Session["logined_id"].ToString()), "schedules", "Edit", LdExpress.GV.cn, LdExpress.GV.trans);

                var result = new
            {
                Message = "sucessful",
                Date = DateTime.Now.ToString("dd-MM-yyyy")
            };
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }

        }

        //LdExpress.Schedules obj = new LdExpress.Schedules();
        //if (ID > 0)
        //{
        //    obj.ID = ID;
        //    obj.Load(LdExpress.GV.cn, LdExpress.GV.trans);

        //    string sqlCommand = "";

        //    sqlCommand = "select ID from tbl_carrs where name ='" + LdExpress.DBModule.RefineString(CarrID) + "' limit 0,1; select ID from tbl_ports where name ='" + LdExpress.DBModule.RefineString(PortToID) + "' limit 0,1; select ID from tbl_ports where name ='" + LdExpress.DBModule.RefineString(PortFromID) + "' limit 0,1;";
        //    System.Data.DataSet ds = LdExpress.DBModule.ExecuteQuery(sqlCommand, LdExpress.GV.cn, LdExpress.GV.trans);

        //    if (ds.Tables[0].Rows.Count > 0)
        //        obj.CarrID = long.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
        //    if (ds.Tables[1].Rows.Count > 0)
        //        obj.PortToID = long.Parse(ds.Tables[1].Rows[0]["ID"].ToString());

        //    if (ds.Tables[2].Rows.Count > 0)
        //        obj.PortFromID = long.Parse(ds.Tables[2].Rows[0]["ID"].ToString());

        //    obj.Mon = Mon;
        //    obj.Tue = Tue;
        //    obj.Web = Web;
        //    obj.Thu = Thu;
        //    obj.Fri = Fri;
        //    obj.Sat = Sat;
        //    obj.Sun = Sun;
        //    obj.TS1 = TS1;
        //    obj.TS2 = TS2;
        //    obj.CY = CY;
        //    obj.CLS = CLS;
        //    obj.Service = Ser;
        //    obj.Import = "";
        //    obj.LastUpdate = DateTime.Now;

        //    obj.Save(LdExpress.GV.cn, LdExpress.GV.trans);
        //    LdExpress.UserObjectsLog.SaveObjectActionLog(long.Parse(context.Session["logined_id"].ToString()), "schedules", "Edit", LdExpress.GV.cn, LdExpress.GV.trans);
        //    var result = new
        //    {
        //        Message = "sucessful",
        //        Date = DateTime.Now.ToString("dd-MM-yyyy")
        //    };
        //    context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
        //}
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}